var router = require('express').Router();
var util = require('util');
var EventProxy = require("eventproxy")

var Consts = require('../consts');
var resultJson = require('../result-json');
var Utils = require('../utils');
var logger = require('../models/logger');
var Comment = require('../models/Comment');
var User = require('../models/User');

router.post('/', function(req, res) {
	res.send('respond with a resource');
});

//根据知识点id查询评论
router.post('/list', function(req, res) {
	var comment = req.body;
	var proxy = new EventProxy();
	proxy.all("results", "count", function(results, count) {
		res.send(resultJson(0, {
			results: results,
			count: count
		}))
	})
	var start = comment.start || 0;
	var count = comment.count || Consts.MAX;
	Comment.find({
			kid: comment.kid
		})
		.skip(start).limit(count).sort({
			"_id": -1
		})
		.exec(function(err, comments) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			var index = 0;
			var results = [];

			comments = JSON.parse(JSON.stringify(comments));
			commentcb(res, index, comments, results, proxy)
		});

	Comment.find({
			kid: comment.kid
		})
		.count(function(err, num) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			proxy.emit("count", num)
		});
});

//评论回调函数
function commentcb(res, index, messages, results, proxy) {
	if (index == messages.length)
		return proxy.emit("results", messages);

	User.findById(messages[index].owner, function(err, user) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		if (!user)
			user = {};

		messages[index].ownerName = user.username;
		messages[index].ownerPhoto = user.photo;
		messages[index].ownerType = user.category;
		index++;
		commentcb(res, index, messages, results, proxy);
	});
};

router.post('/upsert', function(req, res) {
	var comment = req.body;
	comment = JSON.stringify(comment);
	comment = comment.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/</g, '&lt;').replace(/>/g, '&lg;');
	comment = JSON.parse(comment);
	if (!comment.content || !comment.kid)
		return res.send(resultJson(3));

	comment.create = new Date();
	comment.owner = comment._user._id;
	new Comment(comment).save(function(err, comment) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		if (!comment)
			return res.send(resultJson(0, {}));
		comment = JSON.parse(JSON.stringify(comment));
		User.findById(comment.owner, function(err, user) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			if (!user)
				user = {};

			comment.ownerName = user.username;
			comment.ownerPhoto = user.photo;
			comment.ownerType = user.category;
			res.send(resultJson(0, comment));
		});
	});
});

router.post('/remove', function(req, res) {
	var comment = req.body;
	Comment.remove({
		_id: comment._id
	}, function(err) {
		if (err) {
			logger.error(err);
			res.send(resultJson(2));
		} else
			res.send(resultJson(0));
	})
});

module.exports = router;